package com.example.shopping1902.cart;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

import com.example.shopping1902.R;
import com.example.shopping1902.goods.GoodsTypeBean;
import com.example.shopping1902.me.UserBean;
import com.example.shopping1902.utils.BaseActivity;
import com.example.shopping1902.utils.SaveUserUtils;
import com.example.shopping1902.utils.URLUtils;
import com.google.gson.Gson;

import java.util.HashMap;
import java.util.Map;

import static com.example.shopping1902.utils.SaveUserUtils.getUserinfo;

public class PayActivity extends BaseActivity {
    int count;
    GoodsTypeBean.DataBean bean;
    double totalMoney;
    UserBean.DataBean userinfo;
    EditText nameEt,telEt,addressEt,kdEt,totalEt;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_pay);
        initView();
        //接收从上一个页面传来的数据
        Intent intent = getIntent();
         count = intent.getIntExtra("count", 1);
         bean = (GoodsTypeBean.DataBean) intent.getSerializableExtra("info");
        double price = bean.getPrice();
        totalMoney = count*price;
        userinfo = getUserinfo();
        setView();
    }

    private void setView() {
        String name = userinfo.getName();
        if (TextUtils.isEmpty(name)) {
            name = userinfo.getName();
        }
        nameEt.setText(name);
        addressEt.setText(userinfo.getAddress());
        telEt.setText(userinfo.getPhone());
        kdEt.setText("包邮");
        totalEt.setText(totalMoney+"元");
    }
    private void initView() {
        nameEt = findViewById(R.id.pay_et_shr);
        telEt = findViewById(R.id.pay_et_tel);
        addressEt = findViewById(R.id.pay_et_address);
        kdEt = findViewById(R.id.pay_et_ptf);
        totalEt = findViewById(R.id.pay_et_total);
    }

    //获取用户信息的方法
    private UserBean.DataBean getUserinfo(){
        UserBean.DataBean bean = null;
        bean = SaveUserUtils.getUserinfo();
        if (bean == null) {
            bean = SaveUserUtils.getUserToObj(this);
        }
        return bean;
    }
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.pay_iv_write:

                break;
            case R.id.pay_btn_sure:
                Map<String,String>map = new HashMap<>();
                map.put("count",count+"");
                map.put("userId",userinfo.getId()+"");
                map.put("commodityId",bean.getId()+"");
                postNetwork(URLUtils.directPayURL,map);
                //调用支付宝或者微信的sdk
                break;
            case R.id.pay_btn_cancel:
                finish();//取消订单
                break;
            case R.id.pay_daily_back:
                finish();
                break;
        }
    }
    //联网成功时会调用的方法

    @Override
    public void onSuccess(String result) {
        UserBean bean = new Gson().fromJson(result,UserBean.class);
        if (bean.getCode()==200){
            Toast.makeText(this,"购买成功",Toast.LENGTH_SHORT).show();
        }else {
            Toast.makeText(this,"购买失败",Toast.LENGTH_SHORT).show();
        }
    }
}
